package com.ihome.android.market2.asynctask;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ihome.android.EAApp;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.InitialActivity;
import com.ihome.android.academy.R;
import com.ihome.android.market2.info.HomepagecontentEdu;
import com.ihome.android.market2.info.LayoutEdu;
import com.ihome.android.market2.info.NavigationEdu;
import com.ihome.android.market2.info.TopTitleEdu;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.util.SPMgr;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;

/**
 * 实现首页的请求数据接口的实现
 * @author wm
 * @date 2015-08-04
 */
public class HomepagecontentTaskEdu extends AsyncTask<Bundle, Integer, Bundle> {
	private final String					TAG				= HomepagecontentTaskEdu.class.getSimpleName();
	private Context							context;
	private MyProgressDialog				progressDialog;
	private String IMG_PATH=null;

	public HomepagecontentTaskEdu(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (!(context instanceof Activity))
			return;
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.user_grabing_data));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (HomepagecontentTaskEdu.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					HomepagecontentTaskEdu.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = new Bundle();
		String areaNo =SPMgr.getInstance(context).getElement(HttpKeys.KEY_AREANO);

		try {
			String mNavigationEduData = new HttpActions(context).requestHomePage(areaNo);
//			String mNavigationEduData = null;
			LogUtil.d(TAG, "HomepageContentTask--------->mNavigationEduData = \n" + mNavigationEduData);
			if (!TextUtils.isEmpty(mNavigationEduData)) {
				NavigationEdu mNavigationEdu = JsonAnalytic.getInstance().analyseNavigation(mNavigationEduData);
				
				
				if(!TextUtils.isEmpty(mNavigationEdu.getBgimage())){
					//下载背景图片
					IMG_PATH = mNavigationEdu.getBgimage();
				}
				boolean flagIsExist = false;
				for(TopTitleEdu titleEdu : mNavigationEdu.getToptitlelist()){
					if(titleEdu.getName().equals("首页")){
						flagIsExist = true;
					}
				}
				if(!flagIsExist){
					TopTitleEdu titleEdu = new TopTitleEdu();
					titleEdu.setName("首页");
					EAApp.getApp().mTopTitleEduList.clear();
					EAApp.getApp().mTopTitleEduList.add(titleEdu);
					EAApp.getApp().mTopTitleEduList.addAll(mNavigationEdu.getToptitlelist());
				}else{
					EAApp.getApp().mTopTitleEduList = mNavigationEdu.getToptitlelist();
				}
				LogUtil.d(TAG, "mTopTitleEduList = \n" + mNavigationEdu.getToptitlelist().size());
				EAApp.getApp().mHomeContentEduList = mNavigationEdu.getHomepagecontentlist();
				LogUtil.d(TAG, "mHomeContentEduList = \n" + mNavigationEdu.getHomepagecontentlist().size());
			}
			else {
				TopTitleEdu titleEdu = new TopTitleEdu();
				titleEdu.setName("首页");
				EAApp.getApp().mTopTitleEduList.clear();
				EAApp.getApp().mTopTitleEduList.add(titleEdu);
//				EAApp.getApp().mHomeContentEduList.add(new HomepagecontentEdu());
			}

		} catch (ITVException e) {
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		
		//模拟测试数据 
//		EAApp.getApp().mTopTitleEduList.clear();
//		EAApp.getApp().mHomeContentEduList.clear();
//		for(int i =0;i<8;i++){
//			TopTitleEdu titleEdu = new TopTitleEdu();
//			titleEdu.setName("首页"+i);
//			titleEdu.setUrl("www.baidu.com");
//			EAApp.getApp().mTopTitleEduList.add(titleEdu);
//		}
//		
//		for(int i =0;i<8;i++){
//			HomepagecontentEdu titleEdux = new HomepagecontentEdu();
//			LayoutEdu seat = new LayoutEdu();
//			titleEdux.setPlayUrl("http://106.39.160.77/group1/M00/1C/01/aiegTVUBMoOAAu7EAAAAjF6h4-I09.m3u8");
//			titleEdux.setPosterpic("http://localhost:8080/images/1439277483337.png");
//			titleEdux.setType(1);
//			titleEdux.setAppId("12");
//			titleEdux.setFlag("0");
//			titleEdux.setUrl("www.baidu.com");
//			seat.setCol(4);
//			seat.setRow(1);
//			seat.setSize_x(2);
//			seat.setRow(1);
//			titleEdux.setSeat(seat);
//			EAApp.getApp().mHomeContentEduList.add(titleEdux);
//		}
		
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null&& progressDialog.isShowing())
			progressDialog.dismiss();

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
//			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog("",result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")",context.getString(R.string.dialog_ok),new onBtnClickListener() {
					
					@Override
					public void btnRightClickListener(View v) {
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnNeutralClickListener(View v) {
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnLeftClickListener(View v) {
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnCloseClickListener(View v) {
						EAApp.getApp().exitApp(true);
					}
				});
				return;
//			}

		}
		
		if (InitialActivity.class.isInstance(context) && IMG_PATH!=null) {
			((InitialActivity)context).startInitAD(IMG_PATH);			
		}
//		if (InitialActivity.class.isInstance(context)) {
//			((InitialActivity) context).getDataToMain();			
//		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();
	}

}
